package com.atomone.epublib.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.atomone.epublib.ReadApplication;

/**
 * @author liyayun
 */

public class SharedPreUtils {

    private static final String SHARED_NAME = "IReader_pref";

    private static SharedPreUtils instance;
    private SharedPreferences        sp     = null;
    private SharedPreferences.Editor editor = null;

    private SharedPreUtils() {
        sp = ReadApplication.getContext().getSharedPreferences(SHARED_NAME, Context.MODE_MULTI_PROCESS);
        editor = sp.edit();
    }

    public static SharedPreUtils getInstance() {
        if (instance == null) {
            synchronized (SharedPreUtils.class) {
                if (instance == null) {
                    instance = new SharedPreUtils();
                }
            }
        }
        return instance;
    }

    public String getString(String key) {
        return sp.getString(key, "");
    }

    public void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public int getInt(String key, int def) {
        return sp.getInt(key, def);
    }

    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    public boolean getBoolean(String key, boolean def) {
        return sp.getBoolean(key, def);
    }
}
